package me.pinion.service

import me.pinion.model.Comment
import me.pinion.model.ModelType
import me.pinion.model.User
import me.pinion.model.XModel
import me.pinion.model.book.Book
import me.pinion.model.book.Chapter
import me.pinion.model.message.Message
import me.pinion.model.message.MessageType

/**
 * Created by shao on 14-1-12.
 */
class CommentService extends Service{

    MessageService messageService


    /**
     * Create comment
     * @param content comment content
     * @param model comment target
     * @param user comment author
     * @return comment
     */
    public Comment create(String content, XModel<? extends XModel> model, User user){
        Comment comment = new Comment();
        comment.set("author_id", user.getId());
        comment.set("content", content);
        MessageType messageType = MessageType.COMMENT
        User reciver = null


        if (model.getModeType().equals(ModelType.dao.getByClass(Comment.class))){
            comment.setTarget((XModel<? extends XModel>)((Comment)model).getTarget());
            comment.set("parent_id", model.getId());
        }else {
            comment.setTarget(model)
        }

        switch (model.class){
            case Comment.class:
                messageType = MessageType.REPLY
                reciver = (model as Comment).author
                break
            case Book.class:
                reciver = (model as Book).author
                break
            case Chapter.class:
                reciver = (model as Chapter).book.author
                break
        }
        comment.save();

        if (reciver){
            Message message = messageService.sendMessage(user, reciver, messageType, model, comment.content)
        }


        return Comment.dao.findById(comment.getId());
    }
    public Comment create(String content, Class<? extends XModel> clazz, Object id, User user){ return this.create(content, (XModel<? extends XModel>)ModelType.dao.getModel(clazz, id), user); }
    public Comment create(String content, String clazz, Object id, User user){ return this.create(content, (XModel<? extends XModel>)ModelType.dao.getModel(clazz, id), user); }



    /**
     * list recent comments of @param model
     * @param model target
     * @param offset page
     * @param max page size
     * @return comment list
     */
    public List<Comment> recentByModel(XModel model, int offset, int max) {
        return Comment.dao.paginate(offset, max, "SELECT *"
                , " FROM comment WHERE target_id=? AND target_type=? ORDER BY datetime DESC"
                , model.getInt("id")
                , ModelType.dao.getByClass(model.getClass()).getInt("id")
        ).getList();

    }
    public List<Comment> recentByModel(XModel model, int max) { return this.recentByModel(model, 1, max); }
    public List<Comment> recentByModel(XModel model) { return this.recentByModel(model, 20); }

    /**
     *
     * @param user
     * @param offset
     * @param max
     * @return
     */
    public List<Comment> recentByUser(User user, int offset, int max){ throw new RuntimeException("Not finished");}

    /**
     *
     * @param user
     * @param clazz
     * @param id
     * @param offset
     * @param max
     * @return
     */
    public List<Comment> recentOfModelByUser(User user,Class<? extends XModel<?>> clazz, Object id, int offset, int max){ throw new RuntimeException("Not finished");}

    /**
     *
     * @param user
     * @param clazz
     * @param id
     * @param offset
     * @param max
     * @return
     */
    public List<Comment> recentOfModelByUser(User user,String clazz, Object id, int offset, int max){ throw new RuntimeException("Not finished");}

    /**
     *
     * @param user
     * @param model
     * @param offset
     * @param max
     * @return
     */
    public List<Comment> recentOfModelByUser(User user,XModel model, int offset, int max){ throw new RuntimeException("Not finished");}

    /**
     *
     * @param user
     * @param clazz
     * @param offset
     * @param max
     * @return
     */
    public List<Comment> recentOfModelByUser(User user,Class<? extends XModel<?>> clazz, int offset, int max){ throw new RuntimeException("Not finished");}


}
